<?php
    $ConcertDoc = 'performanceList.txt';
    
    if (isset ($_POST['submit']))
    {
        $ConcertFile = fopen($ConcertDoc, 'a+');

        $concertName = $_POST['txtPerformanceName'];
        $concertVenue = $_POST['txtVenueName'];
        $dateAdded = trim($_POST['txtDate']);
        
        //validating if file is empty and writing to it
        if (filesize($urlDoc) == 0)
        {
            //writing to the file
            fwrite($ConcertFile, "$concertName, $concertVenue\r\n");
            echo 'New Entry has been added.';
        }
        else
        {
            //writing to the file
            fwrite($ConcertFile, "$concertName, $concertVenue\r\n");
            echo 'New Entry has been added.';
        }
        fclose($ConcertFile);
    }
    
    
    if (isset ($_POST['table']))
    {
        if (file_exists($ConcertDoc))
        {
            //reading the concerts file into an array
            $concerts = file($ConcertDoc);
            echo '<br><br>';
            echo '<table border = "1" cellpadding = "12">';
            echo '<tr><td><b>Concert Name</b></td>';
            echo '<td><b>Venue</b></td>';
            echo '<td><b>Function</b></td></tr>';
            
            //looping through the concerts array and creating the appropriate associated array
            for ($i = 0; $i < count($concerts); $i++)
            {
                $temp = explode(', ', $concerts[$i]);
                
                //and create the new array
                $concerts[$i] = array('concertName' => $temp[0], 'concertVenue' => $temp[1]);
                
                //outputting the array as a table
                foreach ($concerts as $key => $value) 
                {
                   echo '<tr><td>' . $temp[0] . '</td>';
                   echo '<td>' . $temp[1] . '</td>';
                   echo '<td><a href="deleteevent?hyperlink=' . $temp[0].'"><b>Delete Event</b></a></p></td></tr>';
                   break;
                }
            }
            echo '</table>';
        }
    }
?>